home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 3 / Gekikoh Dennoh Club Vol. 3 (Japan).7z / Gekikoh Dennoh Club Vol. 3 (Japan) (Track 1).bin / games / rifler / c / src.lzh / gl3togbg.c next >
Text File  |  1997-11-05  |  5KB  |  221 lines

  1. /*                                            
  2.         GL3ファイルを、GBGファイル(65536色BGPCG)に変換する。
  3.             VER 1.00
  4.         gl3togbg.x
  5.         a way to compile : "gcc -O gl3togbg.c"
  6.                     BY SJOM
  7.                                             */
  8.  
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13.  
  14. static char nooption_message[]=
  15. "    gl3togbg.x version 1.00            
  16.         by SJOM                \n
  17.     GL3ファイルを、GBGファイルに変換。    
  18.     (GBGは65536色バックグラウンド用    
  19.     PCGファイルです。)            
  20.     実行:gl3togbg.x filename.gl3 quantity    
  21.         filename.gl3    GL3ファイル名
  22.         quantity    変換PCG数    ";
  23.  
  24. #define no_error        0
  25. #define extension_error        1
  26. #define fileopen_error        2
  27. #define nomemory_error        3
  28. #define quantity_error        4
  29. #define filedata_error        5
  30.  
  31. static char *error_message[]={
  32.     "No error !",
  33.     "This extension isn't \"gl3\" !",
  34.     "Cannot open this file !",
  35.     "Lack of memory !",
  36.     "Out of range of a commanded quantity !",
  37.     "There is something strange about this file !"
  38.     };
  39.  
  40.  
  41.  
  42.  
  43. #define G_size        512*512        /* graphic memory word size */
  44. #define null        0x00        /* null code */
  45. #define g_line        28        /* number of pcg data in the line */
  46. #define g_size        18        /* one pcg data length */
  47.  
  48. static int quantity;
  49. static unsigned short *loaded_data;
  50.  
  51.  
  52. /*
  53. #include <sys/iocs.h>
  54. void
  55. escape(void)
  56. {
  57.     if (_iocs_bitsns(0) & 2)
  58.         exit(0);
  59. }
  60. void
  61. test(int data)
  62. {
  63.     printf("TEST:%i\n",data);
  64.     while (!(_iocs_bitsns(6) & 32))
  65.         escape();
  66.     while ((_iocs_bitsns(6) & 32))
  67.         ;
  68. }
  69. */
  70.  
  71. static void
  72. error_exit(int error_no)
  73. {
  74.     puts(error_message[error_no]);
  75.     exit(error_no);
  76. }
  77.  
  78.  
  79. static void *
  80. malloc_check(int size)            /* allocate memory & check memory over error */
  81. {
  82.     void *adress;
  83.     if ((adress=malloc(size))==NULL)
  84.         error_exit(nomemory_error);
  85.     return adress;
  86. }
  87.  
  88.  
  89. static int
  90. tocapital(int ascii)
  91. {
  92.     if ((ascii>='a') & (ascii<='z'))
  93.         ascii-=0x20;
  94.     return ascii;
  95. }
  96.  
  97.  
  98. static int
  99. ext_check(const char *filename,const char *extension)
  100. {
  101.     int loop,ext_len;
  102.     char *point=filename,*ext_sub=extension;
  103.     if (*extension==null)
  104.         return 0;
  105.     for (;;) {                /* "."の位置サーチ */
  106.         if (*point==null)
  107.             return 1;
  108.         if (*point++=='.')
  109.             break;
  110.         /*point++;*/
  111.     };
  112.     for (ext_len=0;ext_len<4;ext_len++) {    /* 拡張子の文字数 */
  113.         if (*ext_sub++==null)
  114.             break;
  115.     };
  116.     for (loop=0;loop<ext_len;loop++) {    /* 拡張子のチェック */
  117.         if ((tocapital(*point)!=tocapital(*extension++)) || (*point==null))
  118.             return 1;
  119.         point++;
  120.     };
  121.     return 0;
  122. }
  123.  
  124.  
  125. static char *
  126. copy_strings(char *base_str)
  127. {
  128.     int loop,length;
  129.     char *copy_str;
  130.     for (length=0;base_str[length]!=null;length++)
  131.         ;
  132.     copy_str=malloc_check(length+1);
  133.     for (loop=0;loop<length+1;loop++)
  134.         copy_str[loop]=*base_str++;
  135.     return copy_str;
  136. }
  137.  
  138.  
  139. static void
  140. change_ext(char *filename,char *extension)
  141. {
  142.     int loop;
  143.     for (loop=0;filename[loop]!='.';loop++)
  144.         ;
  145.     loop++;
  146.     filename[loop++]=*extension++;
  147.     filename[loop++]=*extension++;
  148.     filename[loop]=*extension;
  149. }
  150.  
  151.  
  152. static int
  153. fgetc_check(FILE *stream)
  154. {
  155.     int data;
  156.     data=fgetc(stream);
  157.     if (data==EOF)
  158.         error_exit(filedata_error);
  159.     return data;
  160. }
  161.  
  162.  
  163. static void
  164. fputword(int word,FILE *stream)
  165. {
  166.     fputc(word/256,stream);
  167.     fputc(word & 255,stream);
  168. }
  169.  
  170.  
  171. static void
  172. convert(char *gl3_filename)
  173. {
  174.     int loop,counter,loop_x,loop_y,x_line,y_line,data;
  175.     FILE *gl3_stream,*gbg_stream;
  176.     char *gbg_filename;
  177.     gbg_filename=copy_strings(gl3_filename);
  178.     change_ext(gbg_filename,"GBG");
  179.     if ((gl3_stream=fopen(gl3_filename,"rb"))==NULL)
  180.         error_exit(fileopen_error);
  181.     loaded_data=malloc_check(G_size*2);
  182.     for (loop=0;loop<G_size;loop++) {
  183.         data=fgetc_check(gl3_stream);
  184.         data=data*256+fgetc_check(gl3_stream);
  185.         loaded_data[loop]=data;
  186.     };
  187.     fclose(gl3_stream);
  188.     if ((gbg_stream=fopen(gbg_filename,"wb"))==NULL)
  189.         error_exit(fileopen_error);
  190.     /*converted_data=malloc_check(quantity*16*16*2);*/
  191.     for (counter=0;counter<quantity;counter++) {
  192.         x_line=counter % g_line;
  193.         y_line=counter/g_line;
  194.         for (loop_y=0;loop_y<16;loop_y++)
  195.             for (loop_x=0;loop_x<16;loop_x++) {
  196.                 /* converted_data[counter*16*16+loop_x+loop_y*16]= */
  197.                 data=loaded_data[x_line*g_size+loop_x+(y_line*g_size+loop_y)*512];
  198.                 fputword(data,gbg_stream);
  199.             };
  200.     };
  201.     fclose(gbg_stream);
  202. }
  203.  
  204.  
  205. int
  206. main(int argc,char **argv)
  207. {
  208.     if (argc!=3) {
  209.         puts(nooption_message);
  210.         exit(no_error);
  211.     };
  212.     if (ext_check(argv[1],"GL3"))
  213.         error_exit(extension_error);
  214.     quantity=atoi(argv[2]);
  215.     if ((quantity<1) || (quantity>28*28))
  216.         error_exit(quantity_error);
  217.     convert(argv[1]);
  218.     exit(no_error);
  219. }
  220.  
  221.